<template>
  <div class="carousel-item-container" ref="container" @mousemove="handleMouseMove" @mouseleave="handleMouseLeave">
    <div class="carousel-img" ref="image" :style="imagePosition">
      <ImageLoader
        @load="showWords"
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
      />
    </div>
    <div class="title" ref="title">{{ carousel.title }}</div>
    <div class="desc" ref="desc">{{ carousel.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";
export default {
  props: ["carousel"],
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,
      containersize: null, //外边框尺寸
      innersize: null, //图片尺寸
      mouseX: 0, //鼠标横坐标
      mouseY: 0,  //鼠标纵坐标
    };
  },
  computed: {
    //得到图片坐标
    imagePosition(){
      if(!this.innersize || !this.containersize){
        return;
      }
      const extraWidth = this.innersize.width - this.containersize.width; //图片超出容器的宽度
      const extarHeight = this.innersize.height - this.containersize.height;  //多出的高度
      const left = -(extraWidth / this.containersize.width) * this.mouseX;
      const top = -(extarHeight / this.containersize.height) * this.mouseY

      return{
        transform:`translate(${left}px,${top}px)`,
      }

    },
    center(){
      return{
        x:this.containersize.width / 2,
        y:this.containersize.height /2,
      }
    }
  },
  components: {
    ImageLoader,
  },
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;

    this.getSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.getSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.getSize);
  },
  methods: {
    showWords() {
      this.$refs.title.style.opacity = 1;
      this.$refs.title.style.width = 0;
      //强制渲染
      this.$refs.title.clientWidth; //reflow
      this.$refs.title.style.transition = "1s";
      this.$refs.title.style.width = this.titleWidth + "px";

      this.$refs.desc.style.opacity = 1;
      this.$refs.desc.style.width = 0;
      //强制渲染
      this.$refs.desc.clientWidth; //reflow
      this.$refs.desc.style.transition = "2s 1s";
      this.$refs.desc.style.width = this.descWidth + "px";
    },
    getSize() {
      this.containersize = {
        //获取外容器尺寸
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.innersize = {
        //获取内图片尺寸
        width: this.$refs.image.clientWidth,
        height: this.$refs.image.clientHeight,
      };
    },
    handleMouseMove(e){
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;
    },
    handleMouseLeave(){
      this.mouseX = this.center.x;
      this.mouseY = this.center.y;
    }
  },
};
</script>

<style scoped lang="less">
@import url("~@/style/var.less");
.carousel-item-container {
  width: 100%;
  height: 100%;
  color: #fff;
  position: relative;
  overflow: hidden;
}
.carousel-img {
  width: 110%;
  height: 110%;
  position: absolute;
  left: 0;
  top: 0;
  transition: .3s;
}


.title,
.desc {
  position: absolute;
  left: 30px;
  letter-spacing: 3px;
  text-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), -1px 0 0 rgba(0, 0, 0, 0.5),
    0 1px 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  overflow: hidden;
  opacity: 0;
}
.title {
  top: calc(50% - 40px);
  font-size: 2em;
}
.desc {
  top: calc(50% + 20px);
  font-size: 1.2em;
  color: lighten(@gray, 20%);
}
</style>